//
//  FSCTabBarController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/22.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

class FSCTabBarController: UITabBarController {
    //用于传递左边按钮事件的闭包属性
    var leftBtnClock:( () -> () )?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //设置标签按钮标题的颜色
        tabBar.tintColor = UIColor.colorWithHex(hexColor: 0x00a0e9)
        //模拟网络数据加载
        guard let filePath = Bundle.main.path(forAuxiliaryExecutable: "tabar.plist"),let dictArr = NSArray(contentsOfFile: filePath) else{
            
            return
        }
        
        for dict in dictArr {
            addChildVC(dict as! NSDictionary)
        }
        //添加页面
//        addChildViewController(FSCMessageController(), title: "消息", imageName: "inform")
//        addChildViewController(FSCHomeworkController(), title: "作业", imageName: "homework")
//        addChildViewController(FSCClassRoomController(), title: "课堂", imageName: "class_exercise")
//        addChildViewController(FSCExaminationController(), title: "成绩", imageName: "examination")
        
    }
    
    // 重载添加子控制器的方法
    func addChildVC(_ dict:NSDictionary) {
        
        guard let bundleName = Bundle.main.bundleName,
        //获得字典中的控制器名称
        let vcStr = dict["vc"] as? String,
        
        //将字符串转换为类(反射机制)          //获得带命名空间的类名称
        let classVC = NSClassFromString("\(bundleName).\(vcStr)") as? UIViewController.Type,
        //获得图片名称
        let imageName = dict["imageName"] as? String else {
                return
        }

        //根据装换的类型
        let vc = classVC.init()
        
        vc.title = dict["title"] as? String
        vc.tabBarItem.image = UIImage(named: "\(imageName)_nor")
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_press")?.withRenderingMode(.alwaysOriginal)
        //添加左边按钮
        let leftBtn = UIBarButtonItem(imageName: "icon_menu", target: self, action: #selector(leftBtnClicked))
        vc.navigationItem.leftBarButtonItem = leftBtn
        
        //包装一个导航控制器（自定义）
        let navVc = FSCNavigationController(rootViewController: vc)
        
        addChildViewController(navVc)
        
    }

    //左边导航按钮的监听回调方法
    func leftBtnClicked() -> Void {
        //获得主窗口
        let keyWindow = UIApplication.shared.keyWindow
        keyWindow?.endEditing(true)
        //执行闭包，传递事件
        leftBtnClock?()
    }
    
//    func addChildViewController(_ childController: UIViewController, title:String, imageName:String) {
//        childController.title = title
//        childController.tabBarItem.image = UIImage(named:"\(imageName)_nor")
//        //原色渲染
//        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_press")?.withRenderingMode(.alwaysOriginal)
//        //包装一个导航控制器
//        let navVC = UINavigationController(rootViewController: childController)
//        addChildViewController(navVC)
//
//    }

    

}
